package gw.com.android.presenter;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import gw.com.android.presenter.http.StrategyRequest;
import gw.com.android.utils.CacheUtils;
import www.com.library.interfaces.ReqCallBack;

/**
 * @author jett
 * @since 2017-11-29.
 */
public class HomeAriticlePresenter {

    private final String FILE_NAME = "home_ariticle";

    private boolean first = true;

    private Data mData;

    public void acquireAriticle(final ReqCallBack<HomeAriticlePresenter.Data> callBack) {
        if (first) {
            first = false;
            getFromLocal(callBack);
        }
        new StrategyRequest().getLast(new ReqCallBack<StrategyRequest.ArticleData>() {
            @Override
            public void onReqSuccess(StrategyRequest.ArticleData result) {
                if (result == null || TextUtils.isEmpty(result.id)) {
                    if (callBack != null) {
                        callBack.onReqFailed("");
                    }
                    return;
                }
                if (mData == null || !result.id.equals(mData.ariticleId)) {
                    storeToLocal(result);
                    mData = new Data();
                    mData.ariticleId = result.id;
                    mData.content = TextUtils.isEmpty(result.title) ? result.content : result.title;
                    mData.isNew = true;
                    if (callBack != null) {
                        callBack.onReqSuccess(mData);
                    }
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                if (callBack != null) {
                    callBack.onReqFailed(errorMsg);
                }
            }
        });
    }

    private void storeToLocal(StrategyRequest.ArticleData data) {
        JSONObject json = new JSONObject();
        try {
            json.put("id", data.id);
            json.put("content", TextUtils.isEmpty(data.title) ? data.content : data.title);
            json.put("isNew", true);
            CacheUtils.writeFile(FILE_NAME, json.toString());
        } catch (JSONException ignore) {
        }
    }

    private void getFromLocal(ReqCallBack<HomeAriticlePresenter.Data> callBack) {
        String ariticle = CacheUtils.readFile(FILE_NAME);
        if (!TextUtils.isEmpty(ariticle)) {
            try {
                JSONObject jsonObject = new JSONObject(ariticle);
                mData = new Data();
                mData.ariticleId = jsonObject.optString("id");
                mData.content = jsonObject.optString("content");
                mData.isNew = jsonObject.optBoolean("isNew");
                if (callBack != null) {
                    callBack.onReqSuccess(mData);
                }
            } catch (JSONException ignore) {
            }
        }
    }

    public CharSequence getContent() {
        return mData != null ? mData.content : "";
    }

    public void changeLocalAriticleNotNew() {
        String ariticle = CacheUtils.readFile(FILE_NAME);
        if (!TextUtils.isEmpty(ariticle)) {
            try {
                JSONObject jsonObject = new JSONObject(ariticle);
                if (jsonObject.optBoolean("isNew")) {
                    jsonObject.put("isNew", false);
                    CacheUtils.writeFile(FILE_NAME, jsonObject.toString());
                }
            } catch (JSONException ignore) {
            }
        }
    }

    public class Data {
        public String ariticleId;
        public String content;
        public boolean isNew;
    }

}
